package com.mts.indicatorserver.yearmanage.service.impl


import com.mts.indicatorcommon.yearmanage.vo.YearManageVo
import com.mts.indicatorcommon.yearmanage.search.YearManageSearch
import com.mts.indicatorcommon.yearmanage.converter.YearManageConverter
import com.mts.indicatorjpa.yearmanage.repository.YearManageRepository
import com.mts.indicatorjpa.yearmanage.entity.YearManageEntity
import com.mts.indicatorserver.yearmanage.service.YearManageService
import com.mts.indicatorjpa.yearmanage.entity.QYearManageEntity
import com.mts.javacommon.jpabase.QueryCondition
import com.mts.javacommon.jpabase.ConditionService
import com.mts.javacommon.exception.BizException
import com.mts.javacommon.exception.ExceptionEnum

import com.querydsl.jpa.impl.JPAQuery
import com.querydsl.jpa.impl.JPAQueryFactory
import com.querydsl.core.QueryResults
import com.querydsl.core.types.dsl.StringTemplate
import com.querydsl.core.types.dsl.Expressions
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.PageImpl
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional


/**
 * <pre>
 *      
 * </pre>
 *
 * @author Johnny
 * @date 2025-08-22
 */
@Service
@Transactional(rollbackFor = [Exception::class, RuntimeException::class, Error::class])
        class YearManageServiceImpl(
        private val queryFactory: JPAQueryFactory,
        private val yearManageRepository: YearManageRepository
) : YearManageService, ConditionService<YearManageEntity> {

    private val qYearManageEntity = QYearManageEntity.yearManageEntity

    /**
    * 带条件查询的分页接口
    */
    override fun listByCondition(queryCondition: QueryCondition, pageable: Pageable): Page<YearManageVo> {
        val search = queryCondition as YearManageSearch

        // 查询当前页数据
        val jpaQuery: JPAQuery<YearManageEntity> = queryFactory.select(qYearManageEntity)
                .from(qYearManageEntity)
                .offset((pageable.pageNumber * pageable.pageSize).toLong())
                .limit(pageable.pageSize.toLong())

        fillCondition(jpaQuery, search)
        val list = jpaQuery.fetch()

        // 查询总数
        val countQuery: JPAQuery<Long> = queryFactory.select(qYearManageEntity.count())
                .from(qYearManageEntity)
        val total = countQuery.fetchOne() ?: 0L

        val voList = list.map { YearManageConverter.INSTANCE.domain2vo(it) }
        return PageImpl(voList, pageable, total)
    }


    /**
     * 新增接口
     */
    override fun add(yearManageVo: YearManageVo) {
            yearManageVo.id = null
        val entity = YearManageConverter.INSTANCE.vo2domain(yearManageVo)
            yearManageRepository.save(entity)
    }

    /**
     * 更新接口
     */
    override fun edit(yearManageVo: YearManageVo) {
        if (yearManageRepository.existsById(yearManageVo.id!!)) {
            val entity = YearManageConverter.INSTANCE.vo2domain(yearManageVo)
                yearManageRepository.save(entity)
        } else {
            throw BizException(ExceptionEnum.NOT_FIND_BY_ID)
        }
    }

    /**
     * 删除接口
     */
    override fun delete(id: Long) {
        if (yearManageRepository.existsById(id)) {
                yearManageRepository.deleteById(id)
        } else {
            throw BizException(ExceptionEnum.NOT_FIND_BY_ID)
        }
    }

    /**
     * 批量删除接口
     */
    override fun deletes(ids: List<Long>) {
            yearManageRepository.deleteByIdIn(ids)
    }

    /**
     * 填充查询条件
     */
    override fun fillCondition(jpaQuery: JPAQuery<YearManageEntity>, queryCondition: QueryCondition) {
        val search = queryCondition as YearManageSearch
                if (search.id != null && search.id != 0L) {
                    jpaQuery.where(qYearManageEntity.id.eq(search.id))
                }
                if (!search.name.isNullOrEmpty()) {
                jpaQuery.where(qYearManageEntity.name.eq(search.name))
            }
                if (search.enableController != null) {
                    jpaQuery.where(qYearManageEntity.enableController.eq(search.enableController))
                }
                if (search.yearStart != null && search.yearStart != 0) {
                    jpaQuery.where(qYearManageEntity.yearStart.eq(search.yearStart))
                }
                if (search.yearEnd != null && search.yearEnd != 0) {
                    jpaQuery.where(qYearManageEntity.yearEnd.eq(search.yearEnd))
                }
                if (search.createTime != null) {
                    val dateExpr: StringTemplate = Expressions.stringTemplate("DATE_FORMAT({0},'%Y-%m-%d')", qYearManageEntity.createTime)
                    jpaQuery.where(dateExpr.eq(search.createTime))
                }
                if (search.updateTime != null) {
                    val dateExpr: StringTemplate = Expressions.stringTemplate("DATE_FORMAT({0},'%Y-%m-%d')", qYearManageEntity.updateTime)
                    jpaQuery.where(dateExpr.eq(search.updateTime))
                }
                if (search.createBy != null && search.createBy != 0L) {
                    jpaQuery.where(qYearManageEntity.createBy.eq(search.createBy))
                }
                if (search.updateBy != null && search.updateBy != 0L) {
                    jpaQuery.where(qYearManageEntity.updateBy.eq(search.updateBy))
                }
    }
}
